package array.simple;

/**
 * 山脉数组的峰顶索引
 * 示例 1：
 * 输入：arr = [0,1,0]
 * 输出：1
 *
 * 示例 2:
 * 输入：arr = [0,2,1,0]
 * 输出：1
 *
 * 示例 3：
 * 输入：arr = [0,10,5,2]
 * 输出：1
 *
 * 链接：https://leetcode-cn.com/problems/peak-index-in-a-mountain-array
 */
public class PeakIndexInAMountainArray_852 {

    public int peakIndexInMountainArray(int[] arr) {

        int l = 0, r = arr.length - 1;
        while (l < r) {
            int mid = l + (r - l + 1) >> 1;
            if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) {
                return mid;
            } else if (arr[mid] < arr[mid + 1]) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        PeakIndexInAMountainArray_852 pm = new PeakIndexInAMountainArray_852();
        pm.peakIndexInMountainArray(new int[]{24,69,100,99,79,78,67,36,26,19});

    }
}
